package cn.tedu.test.controller;

import cn.tedu.test.entity.User;
import cn.tedu.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserService userService;

//    @PostMapping("/test")
//    public String regist2(String userName){
//        System.out.println(userName);
//        return "hello";
//    }

    @PostMapping("/regist")
    public String regist(String userName,String pwd){
        userService.regist(userName,pwd);
        return "注册成功！";
    }

    @GetMapping("/login")
    public String login(String userName, String pwd, HttpSession session){
        System.out.println(userName);
        System.out.println(pwd);
        User user = userService.login(userName,pwd);
        session.setAttribute("user",user);
        return "登录成功！";
    }

    @GetMapping
    public List<User> list(Principal principal){
        System.out.println("当前用户:"+principal.getName());
        return userService.list();
    }

}
